package com.example.emergencyattempt01.activity.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Pack_DePackUtil {
    // 用于给用户类网络信息拆包，包装
    // 拆包
    public static void dePackUserAhead(String fromServer, List<Map<String,Object>> itemList)throws JSONException
    {
        // 处理得到的字符串
        JSONArray jsonArray=new JSONArray(fromServer);
        // 在加载之前，先把本表清空，防止出现其他无关数据，此处仅为了保证格式统一
        itemList.clear();
        dePackUserInfo(jsonArray,itemList);
    }
    public static void dePackUserInfo(JSONArray jsonArray,List<Map<String,Object>> itemList) throws JSONException
    {
        for(int i=0;i<jsonArray.length();i++)
        {
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            Map map=new HashMap();
            map.put("userName",jsonObject.getString("userName"));
            map.put("uid",jsonObject.getLong("uid"));
            map.put("userType",jsonObject.getString("userType"));
            map.put("identity",jsonObject.getString("identity"));
            map.put("institute",jsonObject.getString("institute"));
            map.put("ownBooks",jsonObject.getInt("ownBooks"));
            map.put("password",jsonObject.getString("password"));
            itemList.add(map);
        }
    }
    // 装包
    public static String packUserInfo(String userName,String password,String institute,String identity,String userType)throws JSONException
    {
        JSONObject object=new JSONObject();
        object.put("userName",userName);
        object.put("password",password);
        object.put("institute",institute);
        object.put("identity",identity);
        object.put("userType",userType);
        return String.valueOf(object);
    }

    // 用于给借阅记录类网络信息进行拆包
    public static boolean dePackBorrowAhead(String fromServer, List<Map<String,Object>> itemList) throws JSONException
    {
        JSONArray jsonArray=new JSONArray(fromServer);
        JSONObject jsonObject=jsonArray.getJSONObject(0);
        boolean find=jsonObject.getBoolean("find");
        if(find)
        {
            // 先清空原表，再加载
            itemList.clear();
            dePackBorrowInfo(jsonArray,itemList);
            return true;
        }
      else {
            itemList.clear();
            return false;
        }
    }

    private static void dePackBorrowInfo(JSONArray jsonArray, List<Map<String, Object>> itemList)throws JSONException {
        for(int i=0;i<jsonArray.length();i++)
        {
            JSONObject object=jsonArray.getJSONObject(i);
            Map map=new HashMap();
            map.put("uid",object.getLong("uid"));
            map.put("identity",object.getString("identity"));
            map.put("institute",object.getString("institute"));
            map.put("userName",object.getString("userName"));
            map.put("ownBooks",object.getInt("ownBooks"));
            map.put("ISBN",object.getString("ISBN"));
            map.put("bookName",object.getString("bookName"));
            map.put("lid",object.getLong("lid"));
            map.put("bid",object.getLong("bid"));
            map.put("borrowTime",object.getLong("borrowTime"));
            map.put("borrowIsConfirmed",object.getString("borrowIsConfirmed"));
            map.put("returnIsRequested",object.getString("returnIsRequested"));
            map.put("returnIsConfirmed",object.getString("returnIsConfirmed"));
            map.put("continueIsRequested",object.getString("continueIsRequested"));
            map.put("continueIsConfirmed",object.getString("continueIsConfirmed"));


            long returnTime=object.getLong("returnTime");
            long lastTime=returnTime-System.currentTimeMillis();
            map.put("lastTime",lastTime);
            map.put("returnTime",returnTime);

            itemList.add(map);
        }
    }

   // 以下用于向书类信息拆装包使用，待补充
   public static boolean dePackBookAhead(String fromServer,List<Map<String,Object>> itemList)throws JSONException
   {
       JSONArray jsonArray=new JSONArray(fromServer);
       JSONObject jsonObject=jsonArray.getJSONObject(0);
       itemList.clear();
       boolean find=jsonObject.getBoolean("find");
       if (find)
       {
           dePackBookInfo(jsonArray,itemList);
           return  true;
       }
       else
       {
           return false;
       }
   }
    // 以下专供学生查书使用，相比之下，多了一层筛选，对于nowNumber=0的图书，不再入库
    public static boolean dePackBookWithCheck(String fromServer,List<Map<String,Object>> itemList)throws JSONException
    {
        JSONArray jsonArray=new JSONArray(fromServer);
        JSONObject jsonObject=jsonArray.getJSONObject(0);
        itemList.clear();
        boolean find=jsonObject.getBoolean("find");
        if (find)
        {
            dePackBookInfoWithCheck(jsonArray,itemList);
            return  true;
        }
        else
        {
            return false;
        }
    }
    private static void dePackBookInfoWithCheck(JSONArray jsonArray, List<Map<String, Object>> itemList) throws JSONException{
        for (int i=0;i<jsonArray.length();i++)
        {
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            Map map=new HashMap();
            // 得到当前这条信息的在库数目，从而决定到底是否显示出来
            long nowNumber=jsonObject.getLong("nowNumber");
            if(nowNumber!=0)
            {   map.put("ISBN",jsonObject.getString("ISBN"));
                map.put("bookName",jsonObject.getString("bookName"));
                map.put("author",jsonObject.getString("author"));
                map.put("press",jsonObject.getString("press"));
                map.put("introduction",jsonObject.getString("introduction"));
                map.put("nowNumber",jsonObject.getLong("nowNumber"));
                map.put("sumNumber",jsonObject.getLong("sumNumber"));
                map.put("type",jsonObject.getString("type"));
                itemList.add(map);}
        }
    }

    private static void dePackBookInfo(JSONArray jsonArray, List<Map<String, Object>> itemList) throws JSONException{
        for (int i=0;i<jsonArray.length();i++)
        {
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            Map map=new HashMap();
            map.put("ISBN",jsonObject.getString("ISBN"));
            map.put("bookName",jsonObject.getString("bookName"));
            map.put("author",jsonObject.getString("author"));
            map.put("press",jsonObject.getString("press"));
            map.put("introduction",jsonObject.getString("introduction"));
            map.put("nowNumber",jsonObject.getLong("nowNumber"));
            map.put("sumNumber",jsonObject.getLong("sumNumber"));
            map.put("type",jsonObject.getString("type"));
            itemList.add(map);
        }
    }
}
